package com.ccom.test.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.ccom.api.domain.Article;
import com.ccom.api.domain.excel.ArticleExcel;
import com.ccom.core.common.constant.ServiceNameConstants;
import com.ccom.core.common.model.Result;
import com.ccom.test.easyExcel.ArticleExcelListener;
import com.ccom.test.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.Charsets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @author: jjzhang24
 * @date: 2021年12月21日
 */
@Slf4j
@Api(tags = "article 测试")
@RestController
@RequestMapping(ServiceNameConstants.TEST_SERVICE_URL_PREFIX + "/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @ApiOperation(value = "article保存")
    @PostMapping("/save")
    public Result save(@Valid @RequestBody Article article){
        articleService.submit(article);
        return Result.success();
    }

    @ApiOperation(value = "article模板下载")
    @GetMapping("/template")
    public void exportTemplate(HttpServletResponse response) {
        try {
            //获取数据
            List<Article> articleList = new ArrayList<>();
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding(Charsets.UTF_8.name());
            String fileName = URLEncoder.encode("article模板", Charsets.UTF_8.name());
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), ArticleExcel.class).sheet("article数据表").doWrite(articleList);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                response.getOutputStream().close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @ApiOperation(value = "article导出")
    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response) {
        try {
            //获取数据
            List<Article> articleList = articleService.list();
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding(Charsets.UTF_8.name());
            String fileName = URLEncoder.encode("article数据导出", Charsets.UTF_8.name());
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), ArticleExcel.class).sheet("article数据表").doWrite(articleList);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                response.getOutputStream().close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @ApiOperation(value = "article导入")
    @PostMapping("/import")
    public Result importExcel(@RequestParam("file") MultipartFile file) {
        String filename = file.getOriginalFilename();
        if (StringUtils.isEmpty(filename)) {
            throw new RuntimeException("请上传文件!");
        }
        if ((!StringUtils.endsWithIgnoreCase(filename, ".xls") && !StringUtils.endsWithIgnoreCase(filename, ".xlsx"))) {
            throw new RuntimeException("请上传正确的excel文件!");
        }
        InputStream inputStream;
        try {
            ArticleExcelListener importListener = new ArticleExcelListener(articleService);
            inputStream = new BufferedInputStream(file.getInputStream());
            ExcelReaderBuilder builder = EasyExcel.read(inputStream, ArticleExcel.class, importListener);
            builder.doReadAll();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.success();
    }
}
